<?php

namespace Liuhelong\LaravelAdmin\Wechat\Http\Controllers;

use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use \Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountMessageFunction;
use Liuhelong\LaravelAdmin\Wechat\Common;
use Log;

class WechatOffiaccountMessageFunctionController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '消息处理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new WechatOffiaccountMessageFunction());
        //$grid->fixColumns(3, -2);
        $grid->column('id', __('laravel_admin_wechat.Id'))->hide();
        $grid->column('type', __('laravel_admin_wechat.Type'))->using([
			'default'=>'默认',
			'text'=>'文字',
			'image'=>'图片',
			'voice'=>'语音',
			'video'=>'视频',
			'link'=>'链接',
			'file'=>'文件',
			'location'=>'位置',
		]);
        $grid->column('key_type', __('laravel_admin_wechat.Key type'))->using([
			'equal'=>'全匹配',
			'like'=>'简单匹配',
			'reg'=>'正则匹配',
		]);
        $grid->column('key', __('laravel_admin_wechat.Key'));
        $grid->column('reply_type', __('laravel_admin_wechat.Reply type'))->using([
			'text'=>'文字',
			'image'=>'图片',
			'voice'=>'语音',
			'video'=>'视频',
			'news'=>'图文消息'
		]);
        $grid->column('reply_content', __('laravel_admin_wechat.Reply content'))->display(function($reply_content,$column){
            
            return Common::getFile($this->reply_type,$reply_content);
        })->width(300);
        $grid->column('function', __('laravel_admin_wechat.Function'));
        $grid->column('cs_messages', __('laravel_admin_wechat.Cs messages'))->display(function($cs_messages){
            return collect($cs_messages)->map(function($item){
                return $item['name'];
            });
        })->label();
        $grid->column('status', __('laravel_admin_wechat.Status'))->switch();
        $grid->column('is_transfer', __('laravel_admin_wechat.Is transfer'))->switch();
        $grid->column('created_at', __('laravel_admin_wechat.Created at'))->hide();
        $grid->column('updated_at', __('laravel_admin_wechat.Updated at'))->hide();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(WechatOffiaccountMessageFunction::findOrFail($id));

        $show->field('id', __('laravel_admin_wechat.Id'));
        $show->field('type', __('laravel_admin_wechat.Type'));
        $show->field('reply_type', __('laravel_admin_wechat.Reply type'));
        $show->field('reply_content', __('laravel_admin_wechat.Reply content'));
        $show->field('function', __('laravel_admin_wechat.Function'));
        $show->field('status', __('laravel_admin_wechat.Status'));
        $show->field('created_at', __('laravel_admin_wechat.Created at'));
        $show->field('updated_at', __('laravel_admin_wechat.Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new WechatOffiaccountMessageFunction());

        $form->select('type', __('laravel_admin_wechat.Type'))->options([
			'default'=>'默认',
			'text'=>'文字',
			'image'=>'图片',
			'voice'=>'语音',
			'video'=>'视频',
			'link'=>'链接',
			'file'=>'文件',
			'location'=>'位置',
		])->help('建议设定一个默认类型的自动回复作为兜底回复');
		
		
        $form->select('reply_type', __('laravel_admin_wechat.Reply type'))->options([
			'text'=>'文字',
			'image'=>'图片',
			'voice'=>'语音',
			'video'=>'视频',
			'news'=>'图文消息'
		])
		->help("转发客服会导致自动回复无效")
		->when('text', function (Form $form) { 

			$form->textarea('text', __('laravel_admin_wechat.Reply content'));

		})
		->when('in',['image','voice','video'], function (Form $form) { 

			$form->text('reply_media', __('laravel_admin_wechat.Reply media'))->help('请输入素材库中相应素材的资源id');

		})
		->when('news', function (Form $form) { 

			$form->select('news', __('laravel_admin_wechat.Reply content'))->options(\Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountNew::all()->pluck('title','id')->all());

		});
		
        $form->text('function', __('laravel_admin_wechat.Function'))->placeholder('namespace\class@method')->help('附加方法中如果有return，会导致转发客服无效');
        
        $form->checkbox('cs_messages', __('laravel_admin_wechat.Cs messages'))
        ->options(\Liuhelong\LaravelAdmin\Wechat\Models\WechatOffiaccountCsMessage::all()->pluck('name','id')->all())
        ->help('附加的客服消息回复,最多20条');
        $form->switch('status', __('laravel_admin_wechat.Status'))->default(1);

        $form->fieldset('关键词处理(可选)', function (Form $form) {
            $form->select('key_type', __('laravel_admin_wechat.Key type'))->options([
    			'equal'=>'全匹配',
    			'like'=>'简单匹配',
    			'reg'=>'正则匹配',
    		])->help('简单匹配:foo* 匹配 footer');
            $form->text('key', __('laravel_admin_wechat.Key'))->help('正则匹配时这里输入正则表达式(如：/^TEST$/)');
        });
        
        $form->fieldset('转发客服(可选)', function (Form $form) {
            $form->switch('is_transfer', __('laravel_admin_wechat.Is transfer'))->default(0);
            $form->text('customer_service_account', __('laravel_admin_wechat.Customer service account'))->help('如果需要转发给固定的某个客服，这里填完整客服账号');
        });
        return $form;
    }
	
	
}
